<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="移库单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="自动生成" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="源仓库" prop="warehouseId" required>
              <el-select v-model="form.warehouseId" clearable filterable style="width: 100%">
                <el-option
                  v-for="val in storeList"
                  :key="val.id"
                  :label="val.warehouseName"
                  :value="val.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="目标仓库" prop="warehouseId" required>
              <el-select v-model="form.warehouseId" clearable filterable style="width: 100%">
                <el-option
                  v-for="val in storeList"
                  :key="val.id"
                  :label="val.warehouseName"
                  :value="val.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总数量" prop="totalQuantity" required>
              <el-input type="number" v-model="form.totalQuantity" placeholder="请输入" :min="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="总金额" prop="totalAmount" required>
              <el-input v-model="form.totalAmount" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <titleDiv name="产品信息"></titleDiv>
      <div>
        <el-button type="primary" plain @click="list.push({})">新增</el-button>
        <el-button plain @click="list=[]">清空</el-button>
      </div>
      <el-table
        :data="list"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="date" label="产品信息" align="center"></el-table-column>
        <el-table-column prop="date" label="规格信息" align="center"></el-table-column>
        <el-table-column prop="date" label="数量" align="center"></el-table-column>
        <el-table-column prop="date" label="金额" align="center"></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMoveStore, getCommonCode, listStore, listUnit, updateMoveStore} from "../../../../api/imsMng";

export default {
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {},
      titleList: ['新增', '详情', '修改'],
      list: [],
      storeList: [],
      unitList: []
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
      this.load()
      if(!flag){
        getCommonCode({orderType: 50}).then(res => {
          this.$set(this.form, 'orderNo', res.data.orderNo)
        })
      }
    },
    load() {
      listStore().then(res => {
        this.storeList = res.rows
      })
      listUnit().then(res => {
        this.unitList = res.rows
      })
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.details = []
          if (this.flag == 2) {
            updateMoveStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addMoveStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
  }
}
</script>
